$(function() {
	//显示菜单信息
	var head = $("#breadcrumbs");
	head.html('<ul class="breadcrumb">' +
			'<li><i class="icon-home"></i> <a href="/">首页</a><span class="divider"><i class="icon-angle-right"></i></span></li>' +
			'<li><a href="#">绩效管理</a> <span class="divider"><i class="icon-angle-right"></i></span></li>' +
			'<li><a href="/performanceAudit">绩效考核</a> <span class="divider"><i class="icon-angle-right"></i></span></li>' +
			'<li class="active">员工绩效详细信息查看</li></ul>');

	//点击返回按钮
	$("#goBack").click(function() {
		var hisPage = $("#hisPage").val();
		var key = $("#getKey").val();
		key = encodeURIComponent(key);
		window.location.href = "/performanceAudit?hisPage2=" + hisPage + "&key=" + key;
		// window.history.back();
	});

	var perfId = $("#getId").val();

	var init = function() {
		$.ajax({
			url: '/api/performance/selPerfById/' + perfId + '?',
			type: 'get',
			data: {'_t':new Date().getTime()},
			success: function(data) {
				// console.info(data);
				if(data != null && data.perfData != null && data.perfData.length != 0){
					var perfData = data.perfData;
					var termsData = data.termsData;
				 	$("#perfDate").val(perfData.perfDate);
				 	$("#employee").val(perfData.empName);
				 	$("#employeeid").val(perfData.employeeId);
				 	// $("#assessmentid").val(perfData.assessmentPersonId);
				 	// $("#assessmentPerson").val(perfData.assName);
				 	// $("#comment").val(perfData.comment);
				 	// $("#perfScore").html(Math.round(perfData.score*100)/100);
				 	// $("#perfLevel").html(perfData.level);
				 	
				 	getOrgParents(perfData.departmentId, 0);
				 	if(perfData.preDeptId != null) {
				 		$("#preDeptId").val(perfData.preDeptId);
				 		getOrgParents(perfData.preDeptId, 1)
				 	}
				 	if(perfData.project != null) {
				 		$("#project").val(perfData.project);
				 	}
				 	showTerms(termsData,perfData.state);
				}else{
					return;
				}
					
			}
		});
	};

	init();
	var getOrgParents = function(orgId, type) {
	$.ajax({
		url: '/api/organizations/getParents/' + orgId + '?',
		type: 'get',
		data: {'_t':new Date().getTime()},
		success: function(result) {		          
			// console.info(result);
			var parentString = "";
			for(var i = result.length-1; i >= 0; i--){
				if(i != 0) {
					if(result[i].orgName == "分公司") {
						continue;
					}else{
						parentString += result[i].orgName + "/";
					}
					
				}else{
					parentString += result[i].orgName;
				}
			}
			if(type == 1) {
				$("#projectDept").val(parentString);
				// $("#preDeptId").val(result[0].id);
			}else{
				$("#department").val(parentString);
				// $("#deptId").val(result[0].id);
			}

		}
	    });
    };

    var showTerms = function(termsData, perfState) {
		if(termsData != null && termsData.length != 0) {
			$("#tbd_task").html('');
			$("#tbd_pluse").html('');
			$("#tbd_professional").html('');
			var plusNum = 0;
			var professionalNum = 0;
			
			$.each(termsData, function(i, item) {
				// console.info(i + "-----", item.completion);
			    var templateData = {
						term : item.term,
						termId: item.id,
						content : item.content,
					    weight : item.weight+"%",
						completion: item.completion,
						score: item.score,
						name:item.name
					};
				var template;
				if(item.type == 0) {
					//工作事项

				    template = "<tr><td style='text-align:center;width:20%;word-wrap:break-word;vertical-align:middle;'>{{term}}</td>"+
				                   "<td style='text-align:center;width:30%;word-wrap:break-word;vertical-align:middle;'>{{content}}</td>"+
				                   "<td class='hidden-480' style='text-align:center;width:10%;word-wrap:break-word;vertical-align:middle;'>{{weight}}</td>"+
				                   "<td class='hidden-phone' style='text-align:center;width:20%;word-wrap:break-word;vertical-align:middle;'>{{completion}}</td>"+
				                   "<td class='hidden-480' style='text-align:center;width:10%;word-wrap:break-word;vertical-align:middle;'>{{score}}</td>"+
				                   "<td class='hidden-480' style='text-align:center;width:10%;word-wrap:break-word;vertical-align:middle;'>{{name}}</td></tr>";

				    var $html = $(Mustache.to_html(template, templateData));
				    $("#tbd_task").append($html);

				}else if(item.type == 1) {
					//加分项
					plusNum++;
					template = "<tr><td style='text-align:center;width:30%;word-wrap:break-word;vertical-align:middle;'>{{term}}</td>"+
							   "<td style='text-align:center;width:30%;word-wrap:break-word;vertical-align:middle;'>{{content}}</td>"+
							   "<td class='hidden-480' style='text-align:center;width:20%;word-wrap:break-word;vertical-align:middle;'>{{score}}</td>"+
							   "<td class='hidden-480' style='text-align:center;width:20%;word-wrap:break-word;vertical-align:middle;'>{{name}}</td></tr>";

                    var $html = $(Mustache.to_html(template, templateData));
				    $("#tbd_pluse").append($html);
				}else{
					//内容及标准
					professionalNum++;
					template = "<tr><td style='text-align:center;width:30%;word-wrap:break-word;vertical-align:middle;'>{{term}}</td>"+					
							   "<td class='hidden-480' style='text-align:center;width:10%;word-wrap:break-word;vertical-align:middle;'>{{weight}}</td>"+
							   "<td class='hidden-phone' style='text-align:center;width:40%;word-wrap:break-word;vertical-align:middle;'>{{completion}}</td>"+
							   "<td class='hidden-480' style='text-align:center;width:20%;word-wrap:break-word;vertical-align:middle;'>{{score}}</td></tr>";
                    
                    var $html = $(Mustache.to_html(template, templateData));
				    $("#tbd_professional").append($html);
				}
			});
            if(plusNum != 0) {
            	$("#plusTerms").css("display", "block");
            }
            if(professionalNum != 0) {
            	$("#professTerms").css("display", "block");
            }

		}else{
			return;
		}

	};

});